/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercicio.lista3;

import java.util.Random;

/**
 *
 * @author afonso
 */
public class Questao6 {

    public static void main(String[] args) {
        metodoMonteCarlo();
        metodoNumerico();
    }

    public static double f(double x) {
        return Math.exp(x) / (1 - Math.pow(x, 2));
    }

    public static void metodoNumerico() {
        double area = 0;
        double intervalo = 0.0001;

        for (double a = 0; a <= 1 - intervalo; a += intervalo) {
            double b = a + intervalo;
            double alturaA = f(a);
            double alturaB = f(b);
            double base = b - a;
            area += (base * (alturaA + alturaB)) / 2;
        }

        System.out.println("Usando o método númerico: " + area);
    }

    public static void metodoMonteCarlo() {
        int numExp = 100000;
        int cont = 0;
        double xA = 0, xB = 1;
        int yA = 0, yB = 100;
        Random r = new Random();

        for (int i = 0; i < numExp; i++) {
            double x = r.nextDouble();
            double y = r.nextInt(yB + 1);
            if (y <= f(x)) cont++;
        }

        double resp = ((double) cont / numExp) * (xB - xA) * (yB - yA);
        System.out.println("Usando Monte Carlo = " + resp);
    }
}
